﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Reflection;

namespace CommonHelper
{
    public class ConfigHelper
    {
        private static string _Path = Assembly.GetExecutingAssembly().Location;
        private static string _HostPath = _Path.Substring(0, _Path.IndexOf("bin")) + "Websit\\Web.config";
        private static string _ConnectionPath = _Path.Substring(0, _Path.IndexOf("bin")) + "Websit\\Unity.config";

        /// <summary>
        /// 在Config中设置本机的用户名和密码
        /// </summary>
        /// <param name="address">IP地址</param>
        /// <param name="name">用户名</param>
        /// <param name="pass">密码</param>
        public static void SetHostValue(string address, string name, string pass)
        {
            XElement root = XElement.Load(_HostPath);
            var identity = root.Element("system.web").Element("identity");
            identity.SetAttributeValue("userName", name);
            identity.SetAttributeValue("password", pass);
            root.Save(_HostPath);
        }

        /// <summary>
        /// 获取主机的用户名和密码
        /// </summary>
        /// <returns></returns>
        public static IList<string> GetHostValue()
        {
            var hostList = new List<string>();
            XElement root = XElement.Load(_HostPath);
            var identity = root.Element("system.web").Element("identity");
            var name = identity.Attribute("userName").Value;
            var pass = identity.Attribute("password").Value;
            if (CommonMethod.checkString(name, pass))
            {
                hostList.Add(name);
                hostList.Add(pass);
            }

            return hostList;
        }

        /// <summary>
        /// 设置Unity中 数据库字符串连接地址
        /// </summary>
        /// <param name="adderss">数据库服务器的IP地址</param>
        /// <param name="databaseName">数据库名称</param>
        /// <param name="name">用户名</param>
        /// <param name="pass">密码</param>
        /// <param name="port">端口</param>
        public static void SetConnectionStr(string adderss, string databaseName, string name, string pass, string port)
        {
            var connectStr = "Data Source={0};Initial Catalog={1};port={2};User ID={3};Password={4};Pooling=1;SupportNamedParameter=1;minpoolsize=20;maxpoolsize=200;COMMANDTIMEOUT=600;";
            connectStr = string.Format(connectStr, adderss, databaseName, port, name, pass); 
            XElement root = XElement.Load(_ConnectionPath);

            foreach (var element in root.Elements())
            {
                if (element.Name.LocalName == "unity")
                {
                    foreach (var element2 in element.Elements())
                    {
                        if (element2.Name.LocalName == "container")
                        {
                            foreach (var element3 in element2.Elements())
                            {
                                if (element3.Name.LocalName == "register")
                                {
                                    foreach (var element4 in element3.Elements())
                                    {
                                        if (element4.Name.LocalName == "constructor")
                                        {
                                            foreach (var element5 in element4.Elements())
                                            {
                                                if (element5.Name.LocalName == "param" && element5.FirstAttribute.Value == "connectionString")
                                                {
                                                    element5.SetAttributeValue("value", connectStr);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            root.Save(_ConnectionPath); 
        }
    }
}
